package com.dhcc.DRGsHosp.web.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.DRGsHosp.pdfRender.utils.ResourceLoader;
import com.dhcc.checklicense.CreateLicenseTools;

@Controller("CreateLicenseController1234565245")
@RequestMapping("/CreateLicense")
public class CreateLicenseController {
		

    /**
     *  @Discription：生成license
     *  @Author： liuhongjun
     *  @return
     *  @Date:2018年10月22日下午9:04:20
     */
    @ResponseBody
	@RequestMapping(value = "/createLicense")
	public String createLicense(String NAME,String CHECKMACHINECODE ,String MACHINECODE ,String LICENSECODE ,String EXPIREDAY) {
    	CreateLicenseTools.fomStr="[NAME="+NAME+",CHECKMACHINECODE="+CHECKMACHINECODE+",MACHINECODE="+MACHINECODE+",LICENSECODE="+LICENSECODE+",EXPIREDAY="+EXPIREDAY+"]";
    	String aString=CreateLicenseTools.getLicenseFile();
		File folder = new File(new File(ResourceLoader.getPath("")).getParentFile().getParent() + "/download/drglicense");
		if (!folder.exists()) {
			folder.mkdirs();
		}
		String outFilePath=folder.getPath() + File.separator + "drgsLicense.lic";
    	File temp = new File(aString); 	
		File outTemp = new File(outFilePath); 
		if(!outTemp.exists()) {
			try {
				outTemp.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return e.getMessage();
			}
		}
		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		try {
			if(temp.isFile()) {
				try {
				// 2.创建相应的节点流
				FileInputStream fis = new FileInputStream(temp);
				FileOutputStream fos = new FileOutputStream(outTemp);
				bis = new BufferedInputStream(fis);
			    bos = new BufferedOutputStream(fos);
				// 4.实现文件的复制
				byte[] b = new byte[1024];// 每次运20个，可按照实际文件大小调整
				int len;
					while ((len = bis.read(b)) != -1)
					bos.write(b, 0, len);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}finally {
			if (bos != null){
				try{
					bos.close();
				} catch (IOException e){
					e.printStackTrace();
				} finally{
					if (bis != null)
						try{
							bis.close();
						} catch (IOException e){
							e.printStackTrace();
						}
				}
			}
		}
		return "success";
    
    }
 
}
